package frank.zou;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SignonAction extends SignonBaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1686885010546334584L;

	private UserInfo userInfo;

	private List<UserInfo> userInfoList;

	private UserService userService;

	// private static Logger log =
	// Logger.getLogger("frank.zou.SignonAction.class");
	private static Log log = LogFactory.getLog("frank.zou.SignonAction.class");

	public String execute() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("into SignonAction.execute()");
		}
		if (validatedUser(userInfo.getName(), userInfo.getPassword())) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	private boolean validatedUser(String name, String password) {
		// TODO Auto-generated method stub

		// UserService userService = new UserServiceImpl();
		return getUserService().validateUser(name, password);
	}

	public String register() {
		return SUCCESS;
	}

	public String addUser() {
		userService.addUser(userInfo);
		return SUCCESS;
	}

	public String listUser() {
		userInfoList = userService.listUserInfo();
		return SUCCESS;
	}
	
	public String deleteUser() {
		userService.deleteUser(userInfo.getId());
		return SUCCESS;
	}

	private UserService getUserService() {
		// WebApplicationContext ctx =
		// WebApplicationContextUtils.getWebApplicationContext();
		// return (UserService)ctx.getBean("");
		return this.userService;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the userInfoList
	 */
	public List<UserInfo> getUserInfoList() {
		return userInfoList;
	}

	/**
	 * @param userInfoList
	 *            the userInfoList to set
	 */
	public void setUserInfoList(List<UserInfo> userInfoList) {
		this.userInfoList = userInfoList;
	}
}
